<template>
	<view class="contain p-r">
		<view class="h-44 flex padding-lr20 f-a-c p-r" :style="'top:'+getStorage('top')+'px'">
			<view @click="back(1)" class="wrap-btn p-r flex f-a-c f-j-c f24-size b-radius-30 w-36 h-36 van-icon van-icon-arrow-left t-color-w"> </view>
		</view>
		<view class="flex f-a-c f-j-c f12-size t-color-w f-w-500">保证光线充足，不反光</view>
		<view v-if="len != 2" class="flex f-a-c f-j-c f12-size t-color-w f-w-500 margin-t4">舌面伸展，无异色，异物</view>
		<view v-else class="flex f-a-c f-j-c f12-size t-color-w f-w-500 margin-t4">不佩戴眼镜 无刘海阻挡</view>
		<view class="p-r margin-t16" style="height: 50vh;">
			<camera class="flex f-a-c f-j-c" :device-position="isBack ? 'back' : 'front'" flash="off" style="width: 100%; height: 50vh;">
			</camera>
			<cover-view class="flex f-a-c f-j-c h100 wrap-cover">
				<image v-if="len == 0" class="w100" src="@/static/images/1.png" mode="widthFix"></image>
				<image v-if="len == 1" class="w100" src="@/static/images/2.png" mode="widthFix"></image>
				<image v-if="len == 2" class="w100" src="@/static/images/3.png" mode="widthFix"></image>
			</cover-view>
			<!-- <view class="wrap-line"></view> -->
		</view>
		 
		 <view v-if="len == 0" class="flex f-a-c f-j-c f12-size t-color-w f-w-500">将舌头完整放入框内拍摄</view>
		 <view v-if="len == 1" class="flex f-a-c f-j-c f12-size t-color-w f-w-500">将脸部完整放入框内拍摄</view>
		 <view v-if="len == 2" class="flex f-a-c f-j-c f12-size t-color-w f-w-500">将舌头底部完整放入框内拍摄</view>
		 <view class="flex f-a-c f-j-s padding-lr20 margin-t16">
			 <view @click="isBack=!isBack" class="wrap-btn flex f-a-c f-j-c b-radius-30 w-36 h-36 van-icon van-icon-replay t-color-w"> </view>
			 <view @click="takePhoto" class="wrap-btn1"><view class="btn"></view></view>
			 <view @click="uploadImg" class="wrap-btn flex f-a-c f-j-c b-radius-30 w-36 h-36 van-icon van-icon-photo f24-size t-color-w"> </view>
		 </view>
	</view>
</template>
<style scoped>
	.wrap-cover {
		position: absolute;
		inset: 0;
	}
	.contain {
		background-color: #000;
	}
	.wrap-btn {
		background-color: rgba(255,255,255,0.4);
	}
	.wrap-btn1 {
		border-radius: 50%;
		
		border: 8px solid rgba(255, 255, 255, 0.8);
	}
	.wrap-btn1 .btn {
		width: 40px;
		height: 40px;
		border-radius: 50%;
		background-color: #fff;
	}
	.wrap-line {
		position: relative;
		width: 150px;
		height: 160px;
		border: 2px solid #fff;
		border-radius: 5px;
		border-bottom: transparent;
		margin: auto;
	}
	.wrap-line::after {
		content: '';
		position: absolute;
		bottom: -50%;
		left: -2px;
		width: 100%;
		height: 150px;
		border-radius: 50%;
		border: 2px solid #fff;
	}
@import '@/static/css/index/index.css';
</style>
<script>
	// import COS from "cos-nodejs-sdk-v5";
	// const COS = require('cos-nodejs-sdk-v5');
	const $ = require('@/utils/api.js');
	let self = this;
	export default {
		components: {},
		data() {
			return {
				isBack: true,
				len: 0,
				imgArr: [
					// 'http://offline-images.lirenos.com/2383511724598150051',
					// 'http://offline-images.lirenos.com/8728161724597571644',
					// 'http://offline-images.lirenos.com/6336921724597958085'
				],
			};
		},
		onLoad: function(options) {
			self = this;
			this.init();
		},
		onShow() {
		},
		methods: {
			uploadV(file) {
				const self = this;
				const cos = new COS({
					SecretId: "AKIDB4kNq6LYo7MRN5jGf2EMGvbDwUqDTWaT",
					SecretKey: "ujJekeO7fx0rWzFJAp6HMTdnmZ9XifMp",
				});
				// 替换成你的 Bucket 名称和 Region
				const bucket = "jianji-1259268558";
				const region = "ap-shanghai";
				const name =String(parseInt(Math.random()*10000000000000));
				// 生成对象存储桶中的图片路径
				// const key = `images/${file.name}`;
			
				// 将图片上传到腾讯云对象存储桶
				cos.uploadFile({
						Bucket: bucket,
						Region: region,
						Key: name,
						Body: file,
						filePath: file,
					},
					(err, data) => {
						uni.hideLoading();
						if (err) {
							console.error("上传失败：", err);
							$.$toast("上传失败：");
						} else {
							console.log("上传成功：", data.Location);
							const url = 'https://' + data.Location;
							self.imgArr.push(url);
			
						}
					}
				);
			},
			uploadFile(path) {
				uni.showLoading({
					title:'图片上传中'
				})
				uni.uploadFile({
					url: 'https://data.bingdou.site/server/oneyuan/index/upload',
					filePath: path,
					name: 'file',
					header: {
						'Authorization': uni.getStorageSync("token") ? uni.getStorageSync("token") : ''
					},
					formData: {
						token: wx.getStorageSync("token"),
					},
					success: function(res) {
						uni.hideLoading();
						let info = JSON.parse(res.data);
						if(info.code != 1) {
							const url = info.data.url;
							self.imgArr.push(url);
							self.addLen()
						}
					}
				});
			},
			getQs() {
				const self = this;
				uni.showLoading({
					title:'报告分析中…'
				})
				$.ajax({
					url: 'oneyuan/index/createReport',
					data: {
						scene: 1,
						tf_image: this.imgArr[0],//tf_image是舌头, ff_image是面部, tb_image是舌头下
						ff_image: this.imgArr[1],
						tb_image: this.imgArr[2],
					},
					method: 'POST',
					loadingText: '报告分析中…',
					showLoading: true,
					success(res) {
						uni.hideLoading();
						const data = res.data ? res.data : '';
						uni.setStorageSync('qs',data.inquiry_questions);
						self.go('/pages/index/qs?id='+data.session_id);
					},
					error() {
						$.$toast('请重新上传');
						uni.hideLoading();
						self.len = 0;
					}
				})
			},
			addLen() {
				this.len += 1;
				if(this.len > 2) {
					this.getQs();
				}
			},
			uploadImg() {
				const self = this;
				$.uploadimg({
					success(res) {
						self.imgArr.push(res);
						self.addLen()
					}
				})
			},
			 takePhoto() {
			            const ctx = uni.createCameraContext();
			            ctx.takePhoto({
			                quality: 'high',
			                success: (res) => {
								self.uploadFile(res.tempImagePath)
			                }
			            });
			        },
			init() {},
		},
		computed: {},
		created() {
		},
		mounted() {},
		destroyed() {},
		onPullDownRefresh() {
		},
		onReachBottom() {
			
		}
	}
</script>